import time

from selenium.webdriver.common.by import By

from Pages import BasePage

class OBTTrainDetailPage(BasePage.Action):

    def __init__(self):
        #乘客勾选框-未勾选
        self.passengerCheckboxNotChecked_xpath = (By.XPATH, "//div[@class='block passenger']//span[not (contains(@class,'is-checked'))]/span[@class='el-checkbox__inner']")
        #乘客勾选框-已勾选
        self.passengerCheckboxIsChecked_xpath = (By.XPATH, "//div[@class='block passenger']//span[not (contains(@class,'is-checked'))]/span[@class='el-checkbox__inner']")

        #申请改期按钮
        self.applyChangeButton_xpath = (By.XPATH, "//span[@class='change']")
        #确认改期
        self.sureButton_xpath = (By.XPATH, "/html/body/div[3]/div/div[3]/button[2]/span")
        #申请退票按钮
        self.applyRefundButton_xpath = (By.XPATH, "//div[@class='block passenger']//span[text()='申请退票']")
        #确认退票
        self.confirmRefundButton_xpath = (By.XPATH, "//button[@class='confirm']")
        #
        self.confirmRefundButton2_xpath = (By.XPATH, "//button/span[text()='确认退票']")
        #获取订单状态
        self.orderStatus_xpath = (By.XPATH, "//span[@class='order-status']")
        #
        self.copyOrderNo_xpath = (By.XPATH, "//span[text()='复制']")
        #
        self.goChangeOrderDetai_xpath = (By.XPATH, "//div[@class='block passenger']//span[text()='查看改签详情']")


    def checkOrderStatus(self):
        status = self.find_element(*self.orderStatus_xpath).text
        return status

    def choosePassenger(self):
        if self.find_elements(*self.passengerCheckboxIsChecked_xpath):
            print("已选中改签乘客")
            time.sleep(1)
            pass
        else:
            self.find_elements(*self.passengerCheckboxNotChecked_xpath)[0].click()
        time.sleep(1)

    def chooseAllPassenger(self):
        if self.find_elements(*self.passengerCheckboxIsChecked_xpath):
            print("已选中改签乘客")
            time.sleep(1)
            pass
        else:
            for n in self.find_elements(*self.passengerCheckboxNotChecked_xpath):
                n.click()
                time.sleep(1)

    #单人改签or全部改签，全部传0，单人传1
    def applyChange(self, num):
        #等待15s后重试
        if self.checkOrderStatus()=='出票中':
            time.sleep(30)
            self.refresh_window()
            time.sleep(15)
            print("出票中，等待：")
            time.sleep(10)
            return self.applyChange(num)
        elif self.checkOrderStatus()=='已出票':
            print("已出票申请改期：")
            if num==0:
                self.chooseAllPassenger()
            else:
                self.choosePassenger()
            self.smart_click(*self.applyChangeButton_xpath)
            print("申请改期：")
            time.sleep(2)
            self.smart_click(*self.sureButton_xpath)
            print("确认申请改期")
            time.sleep(2)
        else:
            print("其他，结束：")
            pass

    def applyRefund(self):
        print("申请退票")
        self.find_elements(*self.applyRefundButton_xpath)[0].click()
        time.sleep(2)
        self.find_element(*self.confirmRefundButton_xpath).click()
        time.sleep(1)
        self.smart_click(*self.confirmRefundButton2_xpath)
        time.sleep(20)
        self.refresh_window()
        time.sleep(10)

    def doRefund(self):
        if self.checkOrderStatus()=='已出票' or self.checkOrderStatus()=='部分改签':
            self.applyRefund()
        elif self.checkOrderStatus()=='已改签':
            if self.find_elements(*self.goChangeOrderDetai_xpath):
                self.find_elements(*self.goChangeOrderDetai_xpath)[0].click()
                time.sleep(2)
            self.applyRefund()
        elif self.checkOrderStatus()=='改签中':
            time.sleep(30)
            self.refresh_window()
            time.sleep(15)
            print("改签中，等待：")
            time.sleep(10)
            return self.doRefund()

    def getOrderNo(self):
        temp = self.find_element(*self.copyOrderNo_xpath).get_attribute("data-clipboard-text")
        return temp